/*
Author Dave Lenz
Creation date: 16 jun 2009
 */
package nl.ypmits.utils.as3.view.interfaces.wheel {
	import nl.ypmits.utils.as3.math.SimpleMath;

	import de.alex_uhlmann.animationpackage.APCore;

	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.filters.DropShadowFilter;

	public class WheelInterface extends Sprite {

		private var _stage : Stage;
		private var back : Shape;
		private var pijl : WheelArrow;
		private var keydown : Boolean = false;
		private var mouseDeg : Number;
		private var mouseDist : Number;
		private var wheelParts : Array;
		public var showEase : String = "easeInOutSine";
		public var showTime : Number = 1;
		
		private var wheelData : Array = [];
		private var currentPartChosen : WheelPart;

		
		public function WheelInterface( stage : Stage ) {
			_stage = stage;
			addEventListener( Event.ADDED_TO_STAGE, init );
		}
		
		
		private function init(event : Event) : void {
			removeEventListener( Event.ADDED_TO_STAGE, init );

			APCore.initialize( this );
			
			firstDraw();
			
			addListeners( );
			
			filters = [ setBaseDropShadow() ];
		}
		
		
		private function firstDraw() : void {
			back = new Shape( );
			addChild( back );
			
			pijl = new WheelArrow( );
			addChild( pijl );
		}

		
		public function addItem( data : WheelInterfaceVO ) : void {
			wheelData.push( data );
		}

		
		private function addListeners() : void {
			_stage.addEventListener( KeyboardEvent.KEY_DOWN, keyListener );
			_stage.addEventListener( KeyboardEvent.KEY_UP, keyListener );
		}

		
		private function removeListeners() : void {
			_stage.removeEventListener( KeyboardEvent.KEY_DOWN, keyListener );
			_stage.removeEventListener( KeyboardEvent.KEY_UP, keyListener );
		}

		
		/**
		 * 
		 */
		public function start() : void {
			wheelParts = [];
			var totalTime : Number = showTime / wheelData.length;
			for (var i : int = 0; i < wheelData.length ; i++) {
				var part : WheelPart = new WheelPart( i, 360 / wheelData.length, showEase, totalTime );
				part.icon = WheelInterfaceVO( wheelData[i] ).icon;
				addChild( part );
				
				wheelParts.push( part );
				
				part.show( );
			}

			addEventListener( Event.ENTER_FRAME, enterHandler );
		}

		
		/**
		 * 
		 */
		public function stop() : void {
			for (var i : int = 0; i < wheelParts.length ; i++) {
				var part : WheelPart = WheelPart( wheelParts[i] );
				part.clear( );
			}
			
			back.graphics.clear( );
			pijl.clear( );
			removeEventListener( Event.ENTER_FRAME, enterHandler );
		}

		
		private function enterHandler(event : Event) : void {
			mouseDeg = SimpleMath.radiansToDegrees( Math.atan2( mouseY, mouseX ) );
			mouseDist = Math.sqrt( mouseX * mouseX + mouseY * mouseY );
			
			if( mouseDeg < 0) mouseDeg += 360;
			
			pijl.draw( mouseDeg );
			
			for (var i : int = 0; i < wheelParts.length ; i++) {
				var part : WheelPart = WheelPart( wheelParts[i] );
				var rot : Number = part.arc.movieclip.rotation;
				var tr : Number = ( 360 / wheelParts.length );
				if( (( mouseDeg + tr ) > rot && ( mouseDeg + tr ) < (rot + ( 360 / wheelParts.length ) + 1)) ) {
					currentPartChosen = part;
					part.select( );
				} else {
					part.deselect( );
				}
			}
		}

		
		private function keyListener(event : KeyboardEvent) : void {
			if( event.type == KeyboardEvent.KEY_DOWN ) {
				if( (event.keyCode == 32) && (!keydown) ) {
					keydown = true;
					x = _stage.mouseX;
					y = _stage.mouseY;
					start( );
				}
			} else if ( event.type == KeyboardEvent.KEY_UP ) {
				keydown = false;
				if(event.keyCode == 32) {
					stop( );
				}
			}
		}

		
		public static function setBaseDropShadow() : DropShadowFilter {
			return new DropShadowFilter( 4, 45, 0x000000, .35, 5, 5, 1, 3 );
		}

		
		public function dispose() : void {
			removeListeners( );
		}
	}
}
